/*
 * my_task.cpp
 *
 *  Created on: Aug 12, 2010
 *      Author: sdaniele
 */

#include "my_task.h"
#include "flops_job.h"


#include "tbb/mutex.h"
#include "tbb/task_group.h"

typedef ::tbb::mutex mutex_t;

namespace pilot {

mutex_t print_mutex;

#define D 10

my_task::my_task(size_t logflops, ::std::string name, size_t depth)
	:m_logflops(logflops), m_name(name), m_depth(depth)
{}

my_task::~my_task() {}

::tbb::task* my_task::execute(){


		if(m_depth < D){
			set_ref_count(2);
			::tbb::task::spawn( *new(::tbb::task::allocate_child()) ::pilot::my_task(FLOPS,"\\",m_depth+1) );
			::tbb::task::spawn( *new(::tbb::task::allocate_child()) ::pilot::my_task(FLOPS,"#",m_depth+1) );

		}


		flops_job(m_logflops,m_depth," ");

		if(m_depth < D)
			wait_for_all();

		report();

		return (task*)0;
}


void my_task::report() const{
	::std::string depth;
	for(size_t i=0; i<m_depth; i++ )
		depth += "\t";
	{mutex_t::scoped_lock lock(print_mutex);
	::std::cout << m_depth << depth << " [" << m_name << "] reporting 2^" << m_logflops << " flops done" << ::std::endl;}
}

}
